import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { getConnection, Repository } from 'typeorm';
import { createDataDto } from './dto/guangdong.dto';
import { Guangdong } from './guangdong.entity';

@Injectable()
export class GuangdongService {
  constructor(
    @InjectRepository(Guangdong)
    private guangdongRepository: Repository<Guangdong>,
  ) {}

  // 获取总局链路信息
  async getLianluList() {
    const result = await getConnection()
      .createQueryBuilder()
      .select() // 这里添加参数会将每一个返回的字段的名称前面都添加一个job_
      .from(Guangdong, 'shanghai')
      .execute(); // 将获取的数据类型转换为json类型
    return result;
  }

  // 根据链路编号查找链路
  async getLianluInfoByCode(lianlu_code: string) {
    const result = await getConnection()
      .createQueryBuilder()
      .select()
      .from(Guangdong, 'shanghai')
      .where('shanghai.lianlu_code = :lianlu_code', { lianlu_code })
      .execute();
    return result;
  }

  // 根据链路id查找链路
  async getLianluInfoById(id: string) {
    const result = await getConnection()
      .createQueryBuilder()
      .select()
      .from(Guangdong, 'shanghai')
      .where('shanghai.id = :id', { id })
      .execute();
    return result;
  }

  // 新建总局链路信息
  async createLianlu(values: Array<createDataDto>) {
    const result = await getConnection()
      .createQueryBuilder()
      .insert()
      .into(Guangdong)
      .values(values)
      .execute();
    return result;
  }

  // 根据id删除链路信息
  async delLianlu(id: string) {
    const result = await getConnection()
      .createQueryBuilder()
      .delete()
      .from(Guangdong)
      .where('id = :id', { id })
      .execute();
    return result;
  }

  // 修改链路信息
  async updataLianlu(id: string, data) {
    const result = await getConnection()
      .createQueryBuilder()
      .update(Guangdong)
      .set(data)
      .where('id = :id', { id })
      .execute();
    return result;
  }
}
